package com.wuhan.template.module.home.activity;

import android.content.Intent;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseActivity;
import com.wuhan.template.constant.Urls;
import com.wuhan.template.module.home.adapter.HuoDongListAdapter;
import com.wuhan.template.module.home.adapter.HuoDongPageAdapter;
import com.wuhan.template.module.home.entity.ActivityInfoEntity;
import com.wuhan.template.module.home.listener.MyViewPageListener;
import com.wuhan.template.utils.EventMessage;
import com.wuhan.template.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/5/9 0009.
 */
public class ActivityInfoActivity extends BaseActivity {

    @BindView(R.id.back_image)
    protected ImageView backImage;

    protected ViewPager viewPager;

   /* @BindView(R.id.activity_info_recyclerView)
    protected RecyclerView recyclerView;*/

    @BindView(R.id.activity_info_pullListView)
    protected PullToRefreshListView listView;
    protected LinearLayout linearLayout;
    private List<ActivityInfoEntity.ListBean> list = new ArrayList<>();
    private HuoDongListAdapter listAdapter;
    private View header;
    private TextView title;
    private List<ActivityInfoEntity.SlideBean> slide = new ArrayList<>();
    private ImageView imageView;
    private Handler handler = new Handler();
    private int i = 0;

    private int offset = 0;
    private ArrayList<ImageView> imageViews = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home_activity;
    }

    @Override
    protected void initViews() {
        //返回首页的点击方法
        backImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityInfoActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

        AbsListView.LayoutParams layoutParams =
                new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
        header = this.getLayoutInflater().inflate(R.layout.viewpage_layout, listView, false);
        header.setLayoutParams(layoutParams);
        viewPager = (ViewPager) header.findViewById(R.id.activity_info_viewPage);
        title = (TextView) header.findViewById(R.id.activity_title);
        ListView lv = listView.getRefreshableView();
        lv.addHeaderView(header);
        linearLayout = (LinearLayout) header.findViewById(R.id.viewPage_layout);

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                viewPager.setCurrentItem(i++);
                handler.postDelayed(this, 2500);
            }
        }, 2500);


        listAdapter = new HuoDongListAdapter(this, list);
        listView.setAdapter(listAdapter);
        listView.setMode(PullToRefreshBase.Mode.BOTH);

        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                listView.getLoadingLayoutProxy().setRefreshingLabel("正在刷新");
                listView.getLoadingLayoutProxy().setPullLabel("下拉刷新");
                listView.getLoadingLayoutProxy().setReleaseLabel("释放开始刷新");

                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel("最后更新时间:");
                list.clear();
                loadData();

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                listView.getLoadingLayoutProxy().setRefreshingLabel("正在加载");
                listView.getLoadingLayoutProxy().setPullLabel("上拉加载");
                listView.getLoadingLayoutProxy().setReleaseLabel("释放开始加载更多");

                offset += 20;
                loadData();
            }
        });
    }

    @Override
    protected void loadData() {
        OkHttpUtils.get(Urls.HOME_ACTIVITY + "&offset=" + offset, null);
    }

    @Override
    protected void postEventResult(@NonNull EventMessage message) {
        if (message.type.equals(Urls.HOME_ACTIVITY + "&offset=" + offset)) {
            Gson gson = new Gson();
            ActivityInfoEntity infoEntity = gson.fromJson(message.result, ActivityInfoEntity.class);
            slide = infoEntity.getSlide();
            HuoDongPageAdapter adapter = new HuoDongPageAdapter(this, slide);
            viewPager.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            title.setText(slide.get(0).getTitle());
            viewPager.addOnPageChangeListener(new MyViewPageListener() {
                @Override
                public void onPageSelected(int position) {
                    i = position;
                    position %= slide.size();
                    if (position < 0) {
                        position = position + slide.size();
                    }
                    title.setText(slide.get(position).getTitle());
                    viewPager.setCurrentItem(i);

                    imageView.setSelected(false);
                    imageView = imageViews.get(position);
                    imageView.setSelected(true);
                }
            });

                addPoints();

            list.addAll(infoEntity.getList());

            listAdapter.notifyDataSetChanged();

            listView.onRefreshComplete();
        }

    }

    private void addPoints() {
             if(slide == null || slide.isEmpty()){
        return;
    }
        LinearLayout.LayoutParams params =
                new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        for (int i = 0; i < slide.size(); i++) {
            imageView = new ImageView(this);
            imageView.setLayoutParams(params);
            imageView.setSelected(false);
            imageView.setImageResource(R.drawable.selector_nav_points);
            imageViews.add(imageView);
            linearLayout.addView(imageView);
        }
        imageView = imageViews.get(0);
        imageView.setSelected(true);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
